using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Web.Core;
using Web.Core.Objects;
using System.Globalization;

public partial class AdminCP_AdminControls_AddEditMembers : System.Web.UI.UserControl
{
    public string header = "";
    string mDo = "";
    public int mID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {        
        mDo = Request.Params["do"];
        if (mDo != null)
            InitControl();
    }

    void AddSecretQuestion()
    {    
        this.ddlSecretQuestion.Items.Clear();
        CultureInfo temp = null;
        if (Resources.Lang.Culture != null)
            temp = Resources.Lang.Culture;
        Resources.Lang.Culture = new CultureInfo("en");
        this.ddlSecretQuestion.Items.Add(Resources.Lang.SecretQuestionYourPet);
        this.ddlSecretQuestion.Items.Add(Resources.Lang.SecretQuestionYourWord);
        this.ddlSecretQuestion.Items.Add(Resources.Lang.SecretQuestionYourLife);
        Resources.Lang.Culture = new CultureInfo("vi");
        this.ddlSecretQuestion.Items.Add(Resources.Lang.SecretQuestionYourPet);
        this.ddlSecretQuestion.Items.Add(Resources.Lang.SecretQuestionYourWord);
        this.ddlSecretQuestion.Items.Add(Resources.Lang.SecretQuestionYourLife);
        if (temp != null)
            Resources.Lang.Culture = temp;
    }
    void InitControl()
    {
        AddSecretQuestion();

        ArrayList roles = Roles.GetAllRoles();
        foreach (Role r in roles)
            this.ddlRole.Items.Add(r.Name);

        this.rblSex.Items[0].Text = Resources.Lang.Male;
        this.rblSex.Items[1].Text = Resources.Lang.Female;

        int minYear = DateTime.Now.Year - 100;
        int maxYear = DateTime.Now.Year - 10;
        if ((int)Session["Language"] == GlobalStorage.Language.Vietnamese)
            this.scriptEpoch.InnerHtml = "<script type=\"text/javascript\" src=\"../Js/epoch_classes_vi.js\"></script>";
        else if ((int)Session["Language"] == GlobalStorage.Language.English)
            this.scriptEpoch.InnerHtml = "<script type=\"text/javascript\" src=\"../Js/epoch_classes.js\"></script>";
        this.scriptEpoch.InnerHtml += "<script type=\"text/javascript\" language=\"javascript\">var dp_cal; window.onload = function () {dp_cal  = new Epoch('epoch_popup','popup',document.getElementById('" + this.popup_container.ClientID + "'),false," + minYear.ToString() + "," + maxYear.ToString() + ");};</script>";


        if (Session["Membership"] != null && !(Session["Membership"] as Member).IsAdministrator())
            this.btnOK.Visible = false;

        // Validation        
        this.rfvFirstName.ErrorMessage = Resources.Lang.Require;
        this.rfvLastName.ErrorMessage = Resources.Lang.Require;
        this.rfvDateOfBirth.ErrorMessage = Resources.Lang.Require;
        this.rfvAddress.ErrorMessage = Resources.Lang.Require;
        this.rfvEmail.ErrorMessage = Resources.Lang.Require;
        this.rfvPhone.ErrorMessage = Resources.Lang.Require;
        this.RegularExpressionValidatorEmail.ErrorMessage = Resources.Lang.EmailFormat;

        this.RegularExpressionValidatorDate.ErrorMessage = Resources.Lang.RequireDate;
        if ((int)Session["Language"] == GlobalStorage.Language.Vietnamese)
            this.RegularExpressionValidatorDate.ValidationExpression = "\\b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)?[0-9]{2}\\b";
        else if ((int)Session["Language"] == GlobalStorage.Language.English)
            this.RegularExpressionValidatorDate.ValidationExpression = "\\b(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}\\b";

        // Job
        if (Request.Params["mid"] != null)
            mID = int.Parse(Request.Params["mid"]);
        switch (mDo)
        {
            case "edit":
                header = Resources.Lang.Edit;
                Member mem = Members.GetMemberByID(mID);
                if (mem == null)
                    return;

                this.txtEmail.Text = mem.Email;
                if ((int)Session["Language"] == GlobalStorage.Language.Vietnamese)
                    this.popup_container.Text = Utils.ChangeDateFormat(mem.DateOfBirth);
                else
                    this.popup_container.Text = mem.DateOfBirth;
                if (mem.Sex == GlobalStorage.Sex.Male)
                {
                    this.rblSex.Items[0].Selected = true;
                    this.rblSex.Items[1].Selected = false;
                }
                else
                {
                    this.rblSex.Items[1].Selected = true;
                    this.rblSex.Items[0].Selected = false;
                }
                this.txtFirstName.Text = mem.FirstName;
                this.txtLastName.Text = mem.LastName;
                this.txtAddress.Text = mem.Address;
                this.txtPhone.Text = mem.Phone;
                this.ddlSecretQuestion.Text = mem.SQuestion;
                this.ddlRole.Text = mem.Role.Name;

                break;
            case "add":
                header = Resources.Lang.Add;
                break;
            default:
                break;
        }
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        string email = this.txtEmail.Text.Trim();
        string password = this.txtPassword.Text;
        GlobalStorage.Sex sex = GlobalStorage.Sex.Male;
        if (this.rblSex.Items[1].Selected)
            sex = GlobalStorage.Sex.Female;
        string firstName = this.txtFirstName.Text.Trim();
        string lastName = this.txtLastName.Text.Trim();
        string dateOfBirth = this.popup_container.Text.Trim();
        if ((int)Session["Language"] == GlobalStorage.Language.Vietnamese)
            dateOfBirth = Utils.ChangeDateFormat(dateOfBirth);
        string address = this.txtAddress.Text.Trim();
        string phone = this.txtPhone.Text.Trim();
        string sQuestion = ddlSecretQuestion.Text;
        string sAnswer = this.txtSecretAnswer.Text;
        Role role = Roles.GetRole(0, this.ddlRole.Text);

        if (mDo == "edit")
        {
            Member oldMem = Members.GetMemberByID(mID);
            if (password == "")
                password = oldMem.Password;
            else
                password = Utils.Encode(password);
            if (sAnswer == "")
                sAnswer = oldMem.SAnswer;
            else
                sAnswer = Utils.Encode(sAnswer);
            Member updatedMem = new Member(mID, email, password, sex, firstName, lastName, dateOfBirth, address, phone, oldMem.DateAdded, sQuestion, sAnswer, role, oldMem.LastLogin);
            Members.UpdateMember(updatedMem);
        }
        else if (mDo == "add")
        {
            if (this.txtPassword.Text == "")
            {
                this.MsgClient.InnerHtml = "<script language='javascript'>window.alert(" + '"' + Resources.Lang.PasswordRequire + '"' + ");</script>";
                return;
            }
            else if (this.txtSecretAnswer.Text == "")
            {
                this.MsgClient.InnerHtml = "<script language='javascript'>window.alert(" + '"' + Resources.Lang.SAnswerRequire + '"' + ");</script>";
                return;
            }
            else
            {
                string now = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year;
                Member mem = new Member(0, email, Utils.Encode(password), sex, firstName, lastName, dateOfBirth, address, phone, now, sQuestion, Utils.Encode(sAnswer), role, now);
                Members.InsertMember(mem);
            }
        }
        Response.Redirect("Members.aspx");
    }
}
